﻿Imports System.Data.SqlClient

Public Class GestionInterets

    Dim oLocalContext As DBMainContext
    Public oListInteretDejaDansLaListe As New List(Of Interet)


    Private Sub GestionInterets_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        oLocalContext = New DBMainContext
        dgvInteret.DataSource = (From u In oLocalContext.Interet Where Not oListInteretDejaDansLaListe.Contains(u) Select u)

        For Each Col As DataGridViewColumn In dgvInteret.Columns
            If Not New List(Of String)(New String() {"Nom"}).Contains(Col.Name) Then
                Col.Visible = False
            End If
        Next
    End Sub

    Private Sub btnSauvegarder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSauvegarder.Click
        oLocalContext.CustomSubmitChanges()
    End Sub

    Private Sub dgvInteret_RowValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvInteret.RowValidated

        Try
                oLocalContext.SubmitChanges()

        Catch sqlExp As SqlException

            If (sqlExp.Number = 547) Then

                MsgBox("Cet interet ne peut être supprimé, car il est lié à des enregistrements", MsgBoxStyle.Critical)
                CType(Me.Parent, CustomMessageBox).Close()

            End If

        End Try
    End Sub

    Public Function GetSelectedItems() As List(Of Interet)
        Return (From u As DataGridViewRow In dgvInteret.SelectedRows Select CType(u.DataBoundItem, Interet)).ToList

    End Function

End Class